package main

import (
	_ "embed"
	"fmt"
	"log"
	"net/http"
)

//go:embed html/index.html
var htmlIndex []byte

func main() {

	const bindAddr = "0.0.0.0:8080"

	http.HandleFunc("/", handleIndex)
	http.HandleFunc("/api/say", handleApiSay)

	log.Printf("Will server at %s...\n", bindAddr)
	err := http.ListenAndServe(bindAddr, nil)
	if err != nil {
		log.Fatal("Run server failed!\n")
	}
}

// index
func handleIndex(rw http.ResponseWriter, r *http.Request) {

	rw.Write(htmlIndex)
}

// api say
func handleApiSay(rw http.ResponseWriter, r *http.Request) {

	result := []byte(fmt.Sprintf(`{"code":0, "message": "OK", "result":{"message": "hello %s"}}`, r.URL.Query().Get("name")))
	rw.Header().Add("Content-Type", "application/json")
	rw.Write(result)
}
